package cn.tedu.antcolony.passport.controller;

import cn.tedu.antcolony.passport.pojo.param.AdminLoginTime;
import cn.tedu.antcolony.passport.pojo.vo.AdminLoginLogCountVo;
import cn.tedu.antcolony.passport.pojo.vo.AdminLoginLogVo;
import cn.tedu.antcolony.passport.service.IAdminLoginLogService;
import cn.tedu.antcolonycommons.ex.ServiceException;
import cn.tedu.antcolonycommons.pojo.vo.PageData;
import cn.tedu.antcolonycommons.web.JsonResult;
import cn.tedu.antcolonycommons.web.ServiceCode;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.sql.rowset.serial.SerialException;
import java.util.List;

@RestController
@RequestMapping("adminLoginLog")
@Api(tags = "03.管理员登录日志模块")
@Slf4j
@Validated
@CrossOrigin
public class AdminLoginLogController {
    @Autowired
    private IAdminLoginLogService iAdminLoginLogServicep;


    @GetMapping("/list/{pageNum}")
    @ApiOperation("查询管理员登录日志列表")
    @ApiOperationSupport(order = 100)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum",value = "页码",required = false,example = "1",type = "Integer")
    })
    public JsonResult<PageData> list(@Range(min = 1,message = "请提交有效的页码值") @PathVariable("pageNum") Integer pageNum){
        if (pageNum==null){
            pageNum=1;
        }
        PageData<AdminLoginLogVo> adminLoginLogPageData = iAdminLoginLogServicep.listAdminLoginLog(pageNum);
        return JsonResult.ok(adminLoginLogPageData);
    }


    @GetMapping("/adminLoginCount")
    @ApiOperation("查询管理员登录次数")
    @ApiOperationSupport(order = 200)
    public JsonResult<List<AdminLoginLogCountVo>> adminLoginCount(){
        List<AdminLoginLogCountVo> adminLoginLogCountVos = iAdminLoginLogServicep.adminLoginCount();
        return JsonResult.ok(adminLoginLogCountVos);
    }


    /**
     * 根据时间查询管理员登录日志
     * @param pageNum
     * @param adminLoginTime
     * @return
     */
    @GetMapping("/listAdminLoginLogByTime")
    @ApiOperation("根据时间查询管理员登录日志")
    @ApiOperationSupport(order = 300)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum",value = "页码",required = false,example ="1",type ="Integer" )
    })
    public JsonResult<PageData> listAdminLoginLogByTime(@Range(min = 1,message = "请提交有效地页码值") @RequestParam Integer pageNum, AdminLoginTime adminLoginTime){
        if (pageNum==null){
            pageNum=1;
        }
        if (adminLoginTime.getStartTime()==null&&adminLoginTime.getEndTime()==null){
            String message="查询失败，您没提交任何时间参数!";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_BAD_REQUEST,message);
        }
        if (adminLoginTime.getStartTime()==null){
            PageData<AdminLoginLogVo> adminLoginLogVoPageData = iAdminLoginLogServicep.listAdminLoginLogByTimeEndTimeBefore(pageNum, adminLoginTime.getEndTime());
            return JsonResult.ok(adminLoginLogVoPageData);
        }
        if (adminLoginTime.getEndTime()==null){
            PageData<AdminLoginLogVo> adminLoginLogVoPageData = iAdminLoginLogServicep.listAdminLoginLogByTimeStartTimeAfter(pageNum, adminLoginTime.getStartTime());
            return JsonResult.ok(adminLoginLogVoPageData);
        }
        PageData<AdminLoginLogVo> adminLoginLogVoPageData = iAdminLoginLogServicep.listAdminLoginLogByTimeBetween(pageNum, adminLoginTime);
        return JsonResult.ok(adminLoginLogVoPageData);
    }
}
